﻿
Partial Class store_sets
    Inherits CSearchPage

#Region "Data"
    Private m_products As CProductList
    Public ReadOnly Property [Products]() As CProductList
        Get
            If IsNothing(m_products) Then
                m_products = CProduct.Cache.Active.ConferenceSets

                If SpeakerId > 0 Then m_products = m_products.GetBySpeaker1or2Id(SpeakerId)
                If LanguageId > 0 Then m_products = m_products.GetByLanguageId(LanguageId)
                m_products = m_products.Search(Search)

            End If
            Return m_products
        End Get
    End Property
    Protected Overrides ReadOnly Property TypeId As SchemaMikeConnellMinistries.EProductType
        Get
            Return EProductType.ConferenceSet
        End Get
    End Property
#End Region

#Region "Menu"
    Protected Overrides ReadOnly Property TopPanel As System.Web.UI.WebControls.PlaceHolder
        Get
            Return plhTopLinks
        End Get
    End Property
    Protected Overrides ReadOnly Property SubPanel As System.Web.UI.WebControls.PlaceHolder
        Get
            Return plhSubLinks
        End Get
    End Property
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        With Request.FilePath.ToLower
            If .Contains("/store/") OrElse .Contains("/mobile/") OrElse Not .Contains("conference_sets.aspx") Then Response.RedirectPermanent(CSitemap.ConferenceSets(Search, SpeakerId, LanguageId)) 'Renamed page
        End With

        Dim p As CProductList = CProduct.Cache.Active.ConferenceSets
        For Each i As CSpeaker In CSpeaker.Cache.HavingProducts
            Dim count As Integer = p.GetBySpeaker1or2Id(i.SpeakerId).Count
            If count > 0 OrElse SpeakerId = i.SpeakerId Then CDropdown.Add(ddSpeaker, CUtilities.NameAndCount(CUtilities.Truncate(i.SpeakerName), count), i.SpeakerId)
        Next
        CDropdown.BlankItem(ddSpeaker, "-- Any Speaker --", 0)
        CDropdown.SetValue(ddSpeaker, SpeakerId)

        For Each i As CLanguage In CLanguage.Cache
            Dim count As Integer = p.GetByLanguageId(i.LanguageId).Count
            If count > 0 Then CDropdown.Add(ddLanguage, CUtilities.NameAndCount(i.NativeAndName, count), i.LanguageId)
        Next
        CDropdown.BlankItem(ddLanguage, "-- Any Language --", 0)
        CDropdown.SetValue(ddLanguage, LanguageId)

        lnkChinese.NavigateUrl = CSitemap.ConferenceSets(Nothing, 0, ELanguage.Chinese)
        lnkChinese.Text = String.Concat(lnkChinese.Text, " (", CProduct.Cache.Active.ConferenceSets.Chinese.Count, ")")
        lnkEnglish.NavigateUrl = CSitemap.ConferenceSets(Nothing, 0, ELanguage.English)
        lnkEnglish.Text = String.Concat(lnkEnglish.Text, " (", CProduct.Cache.Active.ConferenceSets.Count, ")")
        If LanguageId = ELanguage.Chinese Then
            lnkChinese.ForeColor = Drawing.Color.Black
            lnkChinese.Font.Underline = True
        Else
            lnkEnglish.ForeColor = Drawing.Color.Black
            lnkEnglish.Font.Underline = True
        End If

        'Search state (from querystring)
        txtSearch.Text = Me.Search

        lnkDonate.NavigateUrl = CSitemap.External_Donations
    End Sub
    Protected Overrides Sub PagePreRender()
        'Paging and display
        For Each i As CProduct In ctrlPaging.Display(Products)
            UCSet(plh).Display(i)
        Next

        'Emphasise active filters
        If CDropdown.GetByte(ddSpeaker) > 0 Then ddSpeaker.ForeColor = Drawing.Color.DarkBlue
        If CDropdown.GetByte(ddLanguage) > 0 Then ddLanguage.ForeColor = Drawing.Color.DarkBlue
        If txtSearch.Text.Length > 0 Then txtSearch.ForeColor = Drawing.Color.DarkBlue
    End Sub
#End Region

#Region "Event Handlers - Form"
    Protected Sub btnSearch_Click(sender As Object, e As System.EventArgs) Handles btnSearch.Click
        Response.Redirect(CSitemap.ConferenceSets(txtSearch.Text, CDropdown.GetInt(ddSpeaker), CDropdown.GetInt(ddLanguage)))
    End Sub
    Protected Sub ddLanguage_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddLanguage.SelectedIndexChanged 'Language resets speaker
        Response.Redirect(CSitemap.ConferenceSets(txtSearch.Text, 0, CDropdown.GetInt(ddLanguage)))
    End Sub
    Protected Sub ddSpeaker_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddSpeaker.SelectedIndexChanged 'speaker resets language
        Response.Redirect(CSitemap.ConferenceSets(txtSearch.Text, CDropdown.GetInt(ddSpeaker), 0))
    End Sub
#End Region

#Region "User Controls"
    Private Shared Function UCSet(ByVal target As Control) As store_UCSet
        Dim ctrl As Control = target.Page.LoadControl(CSitemap.Store_UCSet)
        target.Controls.Add(ctrl)
        Return CType(ctrl, store_UCSet)
    End Function
#End Region

End Class

